{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 库包导入\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "from pandas.plotting import autocorrelation_plot                    # 自相关图\n",
    "# 设置字体\n",
    "plt.rcParams['font.sans-serif'] = ['SimHei']\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数据导入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     年份  天然气可供量(亿立方米)  天然气生产量(亿立方米)  进口天然气量(亿立方米)  出口天然气量(-)(亿立方米)  \\\n",
      "0  2021        3773.8        2155.5        1673.5             55.2   \n",
      "1  2020        3340.2        1994.9        1397.0             51.7   \n",
      "2  2019        3057.5        1761.7        1331.8             36.1   \n",
      "3  2018        2814.3        1601.6        1246.4             33.6   \n",
      "4  2017        2390.7        1480.4         945.6             35.3   \n",
      "\n",
      "   年初年末天然气库存差额(亿立方米)  天然气能源消费总量(亿立方米)  农、林、牧、渔业天然气消费总量(亿立方米)  工业天然气消费总量(亿立方米)  \\\n",
      "0                0.0           3773.0                    1.7           2678.2   \n",
      "1                NaN           3339.9                    1.3           2304.0   \n",
      "2                NaN           3059.7                    1.2           2092.1   \n",
      "3                NaN           2817.1                    1.3           1940.1   \n",
      "4                0.0           2393.7                    1.1           1575.2   \n",
      "\n",
      "   建筑业天然气消费总量(亿立方米)  交通运输、仓储和邮政业天然气消费总量(亿立方米)  批发和零售业、住宿和餐饮业天然气消费总量(亿立方米)  \\\n",
      "0               3.2                     366.3                        70.2   \n",
      "1               2.6                     354.3                        62.1   \n",
      "2               2.8                     341.5                        62.5   \n",
      "3               2.5                     286.2                        60.8   \n",
      "4               1.8                     284.7                        57.6   \n",
      "\n",
      "   其他天然气消费总量(亿立方米)  居民生活天然气消费总量(亿立方米)  天然气平衡差额(亿立方米)  \n",
      "0             61.0              592.3            0.8  \n",
      "1             55.6              560.0            0.3  \n",
      "2             57.3              502.3           -2.2  \n",
      "3             57.9              468.4           -2.8  \n",
      "4             52.9              420.3           -3.0  \n",
      "ffill:\n",
      "       年份  天然气可供量(亿立方米)  天然气生产量(亿立方米)  进口天然气量(亿立方米)  出口天然气量(-)(亿立方米)  \\\n",
      "0   2021        3773.8        2155.5        1673.5             55.2   \n",
      "1   2020        3340.2        1994.9        1397.0             51.7   \n",
      "2   2019        3057.5        1761.7        1331.8             36.1   \n",
      "3   2018        2814.3        1601.6        1246.4             33.6   \n",
      "4   2017        2390.7        1480.4         945.6             35.3   \n",
      "5   2016        2080.5        1368.7         745.6             33.8   \n",
      "6   2015        1925.0        1346.1         611.4             32.5   \n",
      "7   2014        1866.8        1301.6         591.3             26.1   \n",
      "8   2013        1706.6        1208.6         525.4             27.5   \n",
      "9   2012        1497.8        1106.1         420.6             28.9   \n",
      "10  2011        1333.0        1053.4         311.5             31.9   \n",
      "11  2010        1082.3         957.9         164.7             40.3   \n",
      "12  2009         896.9         852.7          76.3             32.1   \n",
      "13  2008         816.6         803.0          46.0             32.5   \n",
      "14  2007         706.6         692.4          40.2             26.0   \n",
      "15  2006         566.1         585.5           9.5             29.0   \n",
      "16  2005         463.5         493.2           9.5             29.7   \n",
      "17  2004         390.2         414.6           9.5             24.4   \n",
      "\n",
      "    年初年末天然气库存差额(亿立方米)  天然气能源消费总量(亿立方米)  农、林、牧、渔业天然气消费总量(亿立方米)  \\\n",
      "0                 0.0           3773.0                    1.7   \n",
      "1                 0.0           3339.9                    1.3   \n",
      "2                 0.0           3059.7                    1.2   \n",
      "3                 0.0           2817.1                    1.3   \n",
      "4                 0.0           2393.7                    1.1   \n",
      "5                 0.0           2078.1                    1.1   \n",
      "6                 0.0           1931.8                    0.9   \n",
      "7                 0.0           1870.6                    0.8   \n",
      "8                 0.0           1705.4                    0.7   \n",
      "9                 0.0           1497.0                    0.6   \n",
      "10                0.0           1341.1                    0.6   \n",
      "11                0.0           1080.2                    0.5   \n",
      "12                0.0            895.2                    0.5   \n",
      "13                0.0            812.9                    0.5   \n",
      "14                0.0            705.2                    0.5   \n",
      "15                0.0            573.3                    0.5   \n",
      "16                0.0            466.1                    0.5   \n",
      "17                0.0            396.7                    0.5   \n",
      "\n",
      "    工业天然气消费总量(亿立方米)  建筑业天然气消费总量(亿立方米)  交通运输、仓储和邮政业天然气消费总量(亿立方米)  \\\n",
      "0            2678.2               3.2                     366.3   \n",
      "1            2304.0               2.6                     354.3   \n",
      "2            2092.1               2.8                     341.5   \n",
      "3            1940.1               2.5                     286.2   \n",
      "4            1575.2               1.8                     284.7   \n",
      "5            1338.6               1.9                     254.8   \n",
      "6            1234.5               2.2                     237.6   \n",
      "7            1223.0               1.9                     214.4   \n",
      "8            1129.1               2.0                     175.8   \n",
      "9             980.7               1.3                     154.5   \n",
      "10            875.7               1.3                     138.3   \n",
      "11            691.8               1.2                     106.7   \n",
      "12            577.9               1.0                      91.1   \n",
      "13            531.6               1.0                      71.6   \n",
      "14            479.7               2.1                      46.9   \n",
      "15            398.9               1.7                      44.2   \n",
      "16            327.2               1.5                      38.0   \n",
      "17            278.6               1.4                      26.2   \n",
      "\n",
      "    批发和零售业、住宿和餐饮业天然气消费总量(亿立方米)  其他天然气消费总量(亿立方米)  居民生活天然气消费总量(亿立方米)  \\\n",
      "0                         70.2             61.0              592.3   \n",
      "1                         62.1             55.6              560.0   \n",
      "2                         62.5             57.3              502.3   \n",
      "3                         60.8             57.9              468.4   \n",
      "4                         57.6             52.9              420.3   \n",
      "5                         53.7             48.2              379.7   \n",
      "6                         51.3             45.4              359.8   \n",
      "7                         46.6             41.3              342.6   \n",
      "8                         39.3             35.6              322.9   \n",
      "9                         38.7             32.9              288.3   \n",
      "10                        33.6             27.1              264.4   \n",
      "11                        27.2             26.0              226.9   \n",
      "12                        24.0             23.6              177.7   \n",
      "13                        17.8             20.9              170.1   \n",
      "14                        17.1             16.1              143.4   \n",
      "15                        13.2             12.8              102.6   \n",
      "16                        10.8              9.1               79.4   \n",
      "17                         9.2             14.1               67.2   \n",
      "\n",
      "    天然气平衡差额(亿立方米)  \n",
      "0             0.8  \n",
      "1             0.3  \n",
      "2            -2.2  \n",
      "3            -2.8  \n",
      "4            -3.0  \n",
      "5             2.4  \n",
      "6            -6.7  \n",
      "7            -3.8  \n",
      "8             1.2  \n",
      "9             0.8  \n",
      "10            0.7  \n",
      "11            2.1  \n",
      "12            1.7  \n",
      "13            3.7  \n",
      "14            1.4  \n",
      "15           -7.3  \n",
      "16           -2.6  \n",
      "17           -6.5  \n",
      "bfill:\n",
      "       年份  天然气可供量(亿立方米)  天然气生产量(亿立方米)  进口天然气量(亿立方米)  出口天然气量(-)(亿立方米)  \\\n",
      "0   2021        3773.8        2155.5        1673.5             55.2   \n",
      "1   2020        3340.2        1994.9        1397.0             51.7   \n",
      "2   2019        3057.5        1761.7        1331.8             36.1   \n",
      "3   2018        2814.3        1601.6        1246.4             33.6   \n",
      "4   2017        2390.7        1480.4         945.6             35.3   \n",
      "5   2016        2080.5        1368.7         745.6             33.8   \n",
      "6   2015        1925.0        1346.1         611.4             32.5   \n",
      "7   2014        1866.8        1301.6         591.3             26.1   \n",
      "8   2013        1706.6        1208.6         525.4             27.5   \n",
      "9   2012        1497.8        1106.1         420.6             28.9   \n",
      "10  2011        1333.0        1053.4         311.5             31.9   \n",
      "11  2010        1082.3         957.9         164.7             40.3   \n",
      "12  2009         896.9         852.7          76.3             32.1   \n",
      "13  2008         816.6         803.0          46.0             32.5   \n",
      "14  2007         706.6         692.4          40.2             26.0   \n",
      "15  2006         566.1         585.5           9.5             29.0   \n",
      "16  2005         463.5         493.2           NaN             29.7   \n",
      "17  2004         390.2         414.6           NaN             24.4   \n",
      "\n",
      "    年初年末天然气库存差额(亿立方米)  天然气能源消费总量(亿立方米)  农、林、牧、渔业天然气消费总量(亿立方米)  \\\n",
      "0                 0.0           3773.0                    1.7   \n",
      "1                 0.0           3339.9                    1.3   \n",
      "2                 0.0           3059.7                    1.2   \n",
      "3                 0.0           2817.1                    1.3   \n",
      "4                 0.0           2393.7                    1.1   \n",
      "5                 NaN           2078.1                    1.1   \n",
      "6                 NaN           1931.8                    0.9   \n",
      "7                 NaN           1870.6                    0.8   \n",
      "8                 NaN           1705.4                    0.7   \n",
      "9                 NaN           1497.0                    0.6   \n",
      "10                NaN           1341.1                    0.6   \n",
      "11                NaN           1080.2                    0.5   \n",
      "12                NaN            895.2                    NaN   \n",
      "13                NaN            812.9                    NaN   \n",
      "14                NaN            705.2                    NaN   \n",
      "15                NaN            573.3                    NaN   \n",
      "16                NaN            466.1                    NaN   \n",
      "17                NaN            396.7                    NaN   \n",
      "\n",
      "    工业天然气消费总量(亿立方米)  建筑业天然气消费总量(亿立方米)  交通运输、仓储和邮政业天然气消费总量(亿立方米)  \\\n",
      "0            2678.2               3.2                     366.3   \n",
      "1            2304.0               2.6                     354.3   \n",
      "2            2092.1               2.8                     341.5   \n",
      "3            1940.1               2.5                     286.2   \n",
      "4            1575.2               1.8                     284.7   \n",
      "5            1338.6               1.9                     254.8   \n",
      "6            1234.5               2.2                     237.6   \n",
      "7            1223.0               1.9                     214.4   \n",
      "8            1129.1               2.0                     175.8   \n",
      "9             980.7               1.3                     154.5   \n",
      "10            875.7               1.3                     138.3   \n",
      "11            691.8               1.2                     106.7   \n",
      "12            577.9               1.0                      91.1   \n",
      "13            531.6               1.0                      71.6   \n",
      "14            479.7               2.1                      46.9   \n",
      "15            398.9               1.7                      44.2   \n",
      "16            327.2               1.5                      38.0   \n",
      "17            278.6               1.4                      26.2   \n",
      "\n",
      "    批发和零售业、住宿和餐饮业天然气消费总量(亿立方米)  其他天然气消费总量(亿立方米)  居民生活天然气消费总量(亿立方米)  \\\n",
      "0                         70.2             61.0              592.3   \n",
      "1                         62.1             55.6              560.0   \n",
      "2                         62.5             57.3              502.3   \n",
      "3                         60.8             57.9              468.4   \n",
      "4                         57.6             52.9              420.3   \n",
      "5                         53.7             48.2              379.7   \n",
      "6                         51.3             45.4              359.8   \n",
      "7                         46.6             41.3              342.6   \n",
      "8                         39.3             35.6              322.9   \n",
      "9                         38.7             32.9              288.3   \n",
      "10                        33.6             27.1              264.4   \n",
      "11                        27.2             26.0              226.9   \n",
      "12                        24.0             23.6              177.7   \n",
      "13                        17.8             20.9              170.1   \n",
      "14                        17.1             16.1              143.4   \n",
      "15                        13.2             12.8              102.6   \n",
      "16                        10.8              9.1               79.4   \n",
      "17                         9.2             14.1               67.2   \n",
      "\n",
      "    天然气平衡差额(亿立方米)  \n",
      "0             0.8  \n",
      "1             0.3  \n",
      "2            -2.2  \n",
      "3            -2.8  \n",
      "4            -3.0  \n",
      "5             2.4  \n",
      "6            -6.7  \n",
      "7            -3.8  \n",
      "8             1.2  \n",
      "9             0.8  \n",
      "10            0.7  \n",
      "11            2.1  \n",
      "12            1.7  \n",
      "13            3.7  \n",
      "14            1.4  \n",
      "15           -7.3  \n",
      "16           -2.6  \n",
      "17           -6.5  \n",
      "interpolate:\n",
      "       年份  天然气可供量(亿立方米)  天然气生产量(亿立方米)  进口天然气量(亿立方米)  出口天然气量(-)(亿立方米)  \\\n",
      "0   2021        3773.8        2155.5        1673.5             55.2   \n",
      "1   2020        3340.2        1994.9        1397.0             51.7   \n",
      "2   2019        3057.5        1761.7        1331.8             36.1   \n",
      "3   2018        2814.3        1601.6        1246.4             33.6   \n",
      "4   2017        2390.7        1480.4         945.6             35.3   \n",
      "5   2016        2080.5        1368.7         745.6             33.8   \n",
      "6   2015        1925.0        1346.1         611.4             32.5   \n",
      "7   2014        1866.8        1301.6         591.3             26.1   \n",
      "8   2013        1706.6        1208.6         525.4             27.5   \n",
      "9   2012        1497.8        1106.1         420.6             28.9   \n",
      "10  2011        1333.0        1053.4         311.5             31.9   \n",
      "11  2010        1082.3         957.9         164.7             40.3   \n",
      "12  2009         896.9         852.7          76.3             32.1   \n",
      "13  2008         816.6         803.0          46.0             32.5   \n",
      "14  2007         706.6         692.4          40.2             26.0   \n",
      "15  2006         566.1         585.5           9.5             29.0   \n",
      "16  2005         463.5         493.2           9.5             29.7   \n",
      "17  2004         390.2         414.6           9.5             24.4   \n",
      "\n",
      "    年初年末天然气库存差额(亿立方米)  天然气能源消费总量(亿立方米)  农、林、牧、渔业天然气消费总量(亿立方米)  \\\n",
      "0                 0.0           3773.0                    1.7   \n",
      "1                 0.0           3339.9                    1.3   \n",
      "2                 0.0           3059.7                    1.2   \n",
      "3                 0.0           2817.1                    1.3   \n",
      "4                 0.0           2393.7                    1.1   \n",
      "5                 0.0           2078.1                    1.1   \n",
      "6                 0.0           1931.8                    0.9   \n",
      "7                 0.0           1870.6                    0.8   \n",
      "8                 0.0           1705.4                    0.7   \n",
      "9                 0.0           1497.0                    0.6   \n",
      "10                0.0           1341.1                    0.6   \n",
      "11                0.0           1080.2                    0.5   \n",
      "12                0.0            895.2                    0.5   \n",
      "13                0.0            812.9                    0.5   \n",
      "14                0.0            705.2                    0.5   \n",
      "15                0.0            573.3                    0.5   \n",
      "16                0.0            466.1                    0.5   \n",
      "17                0.0            396.7                    0.5   \n",
      "\n",
      "    工业天然气消费总量(亿立方米)  建筑业天然气消费总量(亿立方米)  交通运输、仓储和邮政业天然气消费总量(亿立方米)  \\\n",
      "0            2678.2               3.2                     366.3   \n",
      "1            2304.0               2.6                     354.3   \n",
      "2            2092.1               2.8                     341.5   \n",
      "3            1940.1               2.5                     286.2   \n",
      "4            1575.2               1.8                     284.7   \n",
      "5            1338.6               1.9                     254.8   \n",
      "6            1234.5               2.2                     237.6   \n",
      "7            1223.0               1.9                     214.4   \n",
      "8            1129.1               2.0                     175.8   \n",
      "9             980.7               1.3                     154.5   \n",
      "10            875.7               1.3                     138.3   \n",
      "11            691.8               1.2                     106.7   \n",
      "12            577.9               1.0                      91.1   \n",
      "13            531.6               1.0                      71.6   \n",
      "14            479.7               2.1                      46.9   \n",
      "15            398.9               1.7                      44.2   \n",
      "16            327.2               1.5                      38.0   \n",
      "17            278.6               1.4                      26.2   \n",
      "\n",
      "    批发和零售业、住宿和餐饮业天然气消费总量(亿立方米)  其他天然气消费总量(亿立方米)  居民生活天然气消费总量(亿立方米)  \\\n",
      "0                         70.2             61.0              592.3   \n",
      "1                         62.1             55.6              560.0   \n",
      "2                         62.5             57.3              502.3   \n",
      "3                         60.8             57.9              468.4   \n",
      "4                         57.6             52.9              420.3   \n",
      "5                         53.7             48.2              379.7   \n",
      "6                         51.3             45.4              359.8   \n",
      "7                         46.6             41.3              342.6   \n",
      "8                         39.3             35.6              322.9   \n",
      "9                         38.7             32.9              288.3   \n",
      "10                        33.6             27.1              264.4   \n",
      "11                        27.2             26.0              226.9   \n",
      "12                        24.0             23.6              177.7   \n",
      "13                        17.8             20.9              170.1   \n",
      "14                        17.1             16.1              143.4   \n",
      "15                        13.2             12.8              102.6   \n",
      "16                        10.8              9.1               79.4   \n",
      "17                         9.2             14.1               67.2   \n",
      "\n",
      "    天然气平衡差额(亿立方米)  \n",
      "0             0.8  \n",
      "1             0.3  \n",
      "2            -2.2  \n",
      "3            -2.8  \n",
      "4            -3.0  \n",
      "5             2.4  \n",
      "6            -6.7  \n",
      "7            -3.8  \n",
      "8             1.2  \n",
      "9             0.8  \n",
      "10            0.7  \n",
      "11            2.1  \n",
      "12            1.7  \n",
      "13            3.7  \n",
      "14            1.4  \n",
      "15           -7.3  \n",
      "16           -2.6  \n",
      "17           -6.5  \n",
      "clear_data_fill:\n",
      "         date  天然气可供量(亿立方米)  天然气生产量(亿立方米)  进口天然气量(亿立方米)  出口天然气量(-)(亿立方米)  \\\n",
      "0 2021-01-01        3773.8        2155.5        1673.5             55.2   \n",
      "1 2020-01-01        3340.2        1994.9        1397.0             51.7   \n",
      "2 2019-01-01        3057.5        1761.7        1331.8             36.1   \n",
      "3 2018-01-01        2814.3        1601.6        1246.4             33.6   \n",
      "4 2017-01-01        2390.7        1480.4         945.6             35.3   \n",
      "\n",
      "   年初年末天然气库存差额(亿立方米)  天然气能源消费总量(亿立方米)  农、林、牧、渔业天然气消费总量(亿立方米)  工业天然气消费总量(亿立方米)  \\\n",
      "0                0.0           3773.0                    1.7           2678.2   \n",
      "1                0.0           3339.9                    1.3           2304.0   \n",
      "2                0.0           3059.7                    1.2           2092.1   \n",
      "3                0.0           2817.1                    1.3           1940.1   \n",
      "4                0.0           2393.7                    1.1           1575.2   \n",
      "\n",
      "   建筑业天然气消费总量(亿立方米)  交通运输、仓储和邮政业天然气消费总量(亿立方米)  批发和零售业、住宿和餐饮业天然气消费总量(亿立方米)  \\\n",
      "0               3.2                     366.3                        70.2   \n",
      "1               2.6                     354.3                        62.1   \n",
      "2               2.8                     341.5                        62.5   \n",
      "3               2.5                     286.2                        60.8   \n",
      "4               1.8                     284.7                        57.6   \n",
      "\n",
      "   其他天然气消费总量(亿立方米)  居民生活天然气消费总量(亿立方米)  天然气平衡差额(亿立方米)  \n",
      "0             61.0              592.3            0.8  \n",
      "1             55.6              560.0            0.3  \n",
      "2             57.3              502.3           -2.2  \n",
      "3             57.9              468.4           -2.8  \n",
      "4             52.9              420.3           -3.0  \n",
      "clear_data_bfill:\n",
      "         date  天然气可供量(亿立方米)  天然气生产量(亿立方米)  进口天然气量(亿立方米)  出口天然气量(-)(亿立方米)  \\\n",
      "0 2021-01-01        3773.8        2155.5        1673.5             55.2   \n",
      "1 2020-01-01        3340.2        1994.9        1397.0             51.7   \n",
      "2 2019-01-01        3057.5        1761.7        1331.8             36.1   \n",
      "3 2018-01-01        2814.3        1601.6        1246.4             33.6   \n",
      "4 2017-01-01        2390.7        1480.4         945.6             35.3   \n",
      "\n",
      "   年初年末天然气库存差额(亿立方米)  天然气能源消费总量(亿立方米)  农、林、牧、渔业天然气消费总量(亿立方米)  工业天然气消费总量(亿立方米)  \\\n",
      "0                0.0           3773.0                    1.7           2678.2   \n",
      "1                0.0           3339.9                    1.3           2304.0   \n",
      "2                0.0           3059.7                    1.2           2092.1   \n",
      "3                0.0           2817.1                    1.3           1940.1   \n",
      "4                0.0           2393.7                    1.1           1575.2   \n",
      "\n",
      "   建筑业天然气消费总量(亿立方米)  交通运输、仓储和邮政业天然气消费总量(亿立方米)  批发和零售业、住宿和餐饮业天然气消费总量(亿立方米)  \\\n",
      "0               3.2                     366.3                        70.2   \n",
      "1               2.6                     354.3                        62.1   \n",
      "2               2.8                     341.5                        62.5   \n",
      "3               2.5                     286.2                        60.8   \n",
      "4               1.8                     284.7                        57.6   \n",
      "\n",
      "   其他天然气消费总量(亿立方米)  居民生活天然气消费总量(亿立方米)  天然气平衡差额(亿立方米)  \n",
      "0             61.0              592.3            0.8  \n",
      "1             55.6              560.0            0.3  \n",
      "2             57.3              502.3           -2.2  \n",
      "3             57.9              468.4           -2.8  \n",
      "4             52.9              420.3           -3.0  \n",
      "clear_data_interpolate:\n",
      "         date  天然气可供量(亿立方米)  天然气生产量(亿立方米)  进口天然气量(亿立方米)  出口天然气量(-)(亿立方米)  \\\n",
      "0 2021-01-01        3773.8        2155.5        1673.5             55.2   \n",
      "1 2020-01-01        3340.2        1994.9        1397.0             51.7   \n",
      "2 2019-01-01        3057.5        1761.7        1331.8             36.1   \n",
      "3 2018-01-01        2814.3        1601.6        1246.4             33.6   \n",
      "4 2017-01-01        2390.7        1480.4         945.6             35.3   \n",
      "\n",
      "   年初年末天然气库存差额(亿立方米)  天然气能源消费总量(亿立方米)  农、林、牧、渔业天然气消费总量(亿立方米)  工业天然气消费总量(亿立方米)  \\\n",
      "0                0.0           3773.0                    1.7           2678.2   \n",
      "1                0.0           3339.9                    1.3           2304.0   \n",
      "2                0.0           3059.7                    1.2           2092.1   \n",
      "3                0.0           2817.1                    1.3           1940.1   \n",
      "4                0.0           2393.7                    1.1           1575.2   \n",
      "\n",
      "   建筑业天然气消费总量(亿立方米)  交通运输、仓储和邮政业天然气消费总量(亿立方米)  批发和零售业、住宿和餐饮业天然气消费总量(亿立方米)  \\\n",
      "0               3.2                     366.3                        70.2   \n",
      "1               2.6                     354.3                        62.1   \n",
      "2               2.8                     341.5                        62.5   \n",
      "3               2.5                     286.2                        60.8   \n",
      "4               1.8                     284.7                        57.6   \n",
      "\n",
      "   其他天然气消费总量(亿立方米)  居民生活天然气消费总量(亿立方米)  天然气平衡差额(亿立方米)  \n",
      "0             61.0              592.3            0.8  \n",
      "1             55.6              560.0            0.3  \n",
      "2             57.3              502.3           -2.2  \n",
      "3             57.9              468.4           -2.8  \n",
      "4             52.9              420.3           -3.0  \n"
     ]
    }
   ],
   "source": [
    "# 导入数据\n",
    "data = pd.read_csv('../RawData/Natural_gas_20.csv', encoding='gbk')\n",
    "# 打印出数据的前5行，检查数据是否导入正确\n",
    "print(data.head())\n",
    "# 对原始数据中的缺失数据进行前项填充\n",
    "clear_data_ffill = data.interpolate(method='ffill', order=3)\n",
    "# 对原始数据中的缺失数据进行后项填充\n",
    "clear_data_bfill = data.interpolate(method='bfill', order=3)\n",
    "# 对原始数据中的缺失数据进行插值填充\n",
    "clear_data_interpolate = data.interpolate(method='linear', order=3)\n",
    "# 打印出行名为风电生产电力量的数据\n",
    "print(\"ffill:\\n\",clear_data_ffill)\n",
    "print(\"bfill:\\n\",clear_data_bfill)\n",
    "print(\"interpolate:\\n\",clear_data_interpolate)\n",
    "# 保存数据\n",
    "clear_data_ffill.to_csv('../ClearData/Natural_gas_20_ffill.csv', index=False)\n",
    "clear_data_bfill.to_csv('../ClearData/Natural_gas_20_bfill.csv', index=False)\n",
    "clear_data_interpolate.to_csv('../ClearData/Natural_gas_20_interpolate.csv', index=False)\n",
    "# 对时间这个列重命名,方便后续操作\n",
    "clear_data_ffill = clear_data_ffill.rename(columns={'年份':'date'})\n",
    "clear_data_ffill['date'] = pd.to_datetime(clear_data_ffill['date'], format='%Y')\n",
    "# 对bfill进行同样的操作\n",
    "clear_data_bfill = clear_data_bfill.rename(columns={'年份':'date'})\n",
    "clear_data_bfill['date'] = pd.to_datetime(clear_data_bfill['date'], format='%Y')\n",
    "# 对interpolate进行同样的操作\n",
    "clear_data_interpolate = clear_data_interpolate.rename(columns={'年份':'date'})\n",
    "clear_data_interpolate['date'] = pd.to_datetime(clear_data_interpolate['date'], format='%Y')\n",
    "# 打印出数据的前5行\n",
    "print(\"clear_data_fill:\\n\",clear_data_ffill.head())\n",
    "print(\"clear_data_bfill:\\n\",clear_data_bfill.head())\n",
    "print(\"clear_data_interpolate:\\n\",clear_data_interpolate.head())\n",
    "# 保存数据,三种填充方式的数据\n",
    "clear_data_ffill.to_csv('../ClearData/Natural_gas_20_ffill_date.csv', index=False)\n",
    "clear_data_bfill.to_csv('../ClearData/Natural_gas_20_bfill_date.csv', index=False)\n",
    "clear_data_interpolate.to_csv('../ClearData/Natural_gas_20_interpolate_date.csv', index=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "tf",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
